// 导入路由插件
import VueRouter from 'vue-router'
import Vue from 'vue'

// 使用插件 - 重要
Vue.use(VueRouter)

// 导入组件
// 一级路由
import Domusi from '../49.模拟网易云/view/Domusi.vue'
import Mymusic from '../49.模拟网易云/view/Mymusic.vue'
// 二级路由
import recommend from '../49.模拟网易云/view/recommend.vue'
import ranking from '../49.模拟网易云/view/ranking.vue'
import song from '../49.模拟网易云/view/song.vue'
import Listing from '../49.模拟网易云/view/Listing.vue'
import loveSong from '../49.模拟网易云/view/loveSong.vue'
import Mysong from '../49.模拟网易云/view/Mysong.vue'
import login from '../49.模拟网易云/view/login.vue'



// 创建路由规则
const router = new VueRouter({
  routes: [
    {
      path:'/login',
      component:login,
    },
    {
      path:'/',
      component:Domusi,
    },
    {
      path:'/Domusi',
      component:Domusi,
     
      children:[
        {
          path:'/Domusi/recommend',
          component:recommend,
        },
       
        // {
        //   path:'/',
        //   component:recommend,
        // },
        {
          path:'/Domusi/ranking',
          component:ranking,
        },
        {
          path:'/Domusi/song',
          component:song,
        }
      ]
    },
    {
      path:'/Mymusic',
      component:Mymusic,
      children:[
        {
          path:'/Listing',
          component:Listing,
        },
      
        {
          path:'',
          component:Listing,
        },
        {
          path:'/loveSong',
          component:loveSong,
        },
        {
          path:'/Mysong',
          component:Mysong,
        }
      ]
    },
    
  ]
})
router.beforeEach((to,from,next)=>{
   // to代表要跳转到哪个路径去, to的值是个对象可以打印看到
  // from代表从哪个路径跳过去
  // next是一个函数。 
  //   next()             放行；
  //   next('路由地址')    跳到指定的位置

  // 判断，是否有权限去访问页面，如果没有权限，跳转到登录页
  const token = localStorage.getItem('token') //假设token
  if(to.path==='/login'){
    next()
  }else{
    if(token){
      next()
    }else{
      next('/login')
    }
  }
})

export default router